using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Elementum
{
    static public class StageManager
    {

        static Dictionary<string, Stage> _prototypeTable = new Dictionary<string, Stage>();
        static public GameClass Game { get; set; }
        static public string LastStageID = null;
        static public DataNeedSave Save = null;
        static ProtalCommand _pc = null;
        static public bool GameClear = false;


        static public string CurrentStateID
        {
            get;
            private set;
        }

        static public ProtalCommand ProtalCommand
        {
            get
            {
                return _pc;
            }
            set
            {
                if (_pc == null)
                    _pc = value;
            }
        }

        static public void AddStagePrototype(string id, Stage stage)
        {
            _prototypeTable.Add(id, stage);
        }

        static public bool HasSave {
            get { return Save.LastLocation != null; }
        }

        static public void Initialize()
        {
            if (Save.LastLocation != null)
            {
                //if (Game.CurrentStage!=null)
                Game.CurrentStage.Restart();
                ProtalCommand = Save.LastLocation;
                LastStageID = ProtalCommand.StageID;
            }
            
        }

        static public void ReLoadStage() {
            ProtalCommand = Save.LastLocation;
            if (CurrentStateID!=null&&Save.StageStateTable.ContainsKey(CurrentStateID))
            {
                StageState stageState = new StageState();
                Save.StageStateTable[CurrentStateID] = stageState;
            }
        }

        static public void GoToStage()
        {

            if (GameClear) {
                DisposeOldStage();
                Game.CurrentStage = new EndStage(Game).Clone();
                GameClear = false;
                return;
            }

            if (ProtalCommand == null)
                return;

            string stageId = ProtalCommand.StageID;

            DisposeOldStage();

            if (LastStageID != null)
            {
                stageId = LastStageID;
                LastStageID = null;
            }

            if (!_prototypeTable.ContainsKey(stageId))
                throw new Exception("stage:" + stageId + "not found!!");

            if (_prototypeTable.ContainsKey(stageId)&&(Game.CurrentStage==null))
            {
                CurrentStateID = stageId;
                if (!Save.StageStateTable.ContainsKey(stageId))
                {
                    StageState stageState = new StageState();
                    try{Save.StageStateTable.Add(stageId, stageState);}
                    catch (ArgumentException){throw new Exception("An StateState with Key = " + stageId + " already exists.");}
                }

                Stage stage = _prototypeTable[stageId].Clone();
                Game.CurrentStage = stage;
                StageState st = Save.StageStateTable[CurrentStateID];
                Vector2 pos = st.ItemStateTable[ProtalCommand.ProtalID].Position;
                stage.Camera._position = pos;
                stage.player.Position = pos;
                stage.player.IniPosition = pos;
                Save.LastLocation = ProtalCommand;
                _pc = null;
            }
           

        }

        public static void DisposeOldStage()
        {
            if (Game.CurrentStage != null)
            {
                Stage preStage = null;
                ResourceManager.Dispose();
                preStage = Game.CurrentStage;
                preStage.Dispose();
                Game.CurrentStage = null;
            }
        }





    }
}
